<?php
namespace app\api\service;
/**
 * UserNumber Service
 * User: zng
 * Date: 2019/10/24
 * Time: 16:25
 */
use app\api\model\user\UserNumber as UserNumberModel;

class UserNumber {

    /**
     * 检测unionid是否存在,并绑定
     * @author zx
     * @date 2019-10-24
     * @return array
     */
    public function unionidBinding(string $unionid=NULL, string $openid, string $state)
    {
        $openidfield = $state.'_openid';
        $userNumberRes = (new UserNumberModel())->where($openidfield, $openid)->findOrEmpty();
        if ( $userNumberRes->isEmpty() === false ) {
            if ( $unionid && ! $userNumberRes->unionid ) {
                $save = (new UserNumberModel())->isUpdate()->save(['unionid'=>$unionid], ['id'=>$userNumberRes->id]);
                if ( $save === false ) {
                    return returnArray([], 'unionid绑定失败', 500);
                }
                $res = $userNumberRes->toArray();
                $res['unionid'] = $unionid;
                return returnArray($res, '绑定成功');
            }
        } else {
            //判断unionid是否存在
	        if ( $unionid ) {
                $userNumberRes = (new UserNumberModel())->where('unionid', $unionid)->findOrEmpty();
	        }
            if ($unionid && isset($userNumberRes) && $userNumberRes->isEmpty() === false ) {
                $save = (new UserNumberModel())->isUpdate()->save([$openidfield=>$openid], ['id'=>$userNumberRes->id]);
                if ( $save === false ) {
                    return returnArray([], 'openid绑定失败', 500);
                }
                $res = $userNumberRes->toArray();
                $res['openid'] = $openid;
                return returnArray($res, '绑定成功');
            }
            $create = UserNumberModel::create([
                $openidfield => $openid,
                'number' => $this->getNumbers(),
                'unionid' => $unionid
            ]);
            if ( $create->isEmpty() === false ) {
                return returnArray($create->toArray(), '注册成功');
            }
            return returnArray([], '注册失败', 500);
        }
        return returnArray($userNumberRes->toArray(), '用户已存在', 200);
    }

    /**
     * 新用户生成用户账号
     * @author zx
     * @date 2019-10-24
     * @return int
     */
    private function getNumbers():int
    {
        $lastNumber = (new UserNumberModel())->field('number')->order('number', 'desc')->findOrEmpty();
        $number = 132001;
        if ( $lastNumber->isEmpty() === false ) {
            $number = $lastNumber->number;
        }
        return $this->getAvailableNumber($number);
    }

    /**
     * 验证是否可用并返回可用
     *
     * @param int $number
     * @author zx
     * @date 2019-10-24
     * @return int
     */
    private function getAvailableNumber(int $number):int
    {
        $v = [222222,333333,444444,555555,666666,777777,888888,999999,123456,234567,345678,456789,987654,876543,765432,654321,543210,567890];
        $number++;
        if(in_array($number, $v)){
            $number = $this->getAvailableNumber($number);
        }
        return $number;
    }

    /**
     * 编辑语种
     *
     */
    public function language( int $language, int $uid ):bool
    {
        return (new UserNumberModel())->isUpdate()->save(['language'=>$language], ['id'=>$uid]);
    }

}
